package com.mycompany.services.dao;

import java.security.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.text.html.HTMLDocument.Iterator;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.mycompany.optimizaproject.Incidente;
import com.mycompany.optimizaproject.Solucion;

public class IncidenteDAO extends AbstractHibernateDAO<Incidente, Integer> {

	public IncidenteDAO(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}
	@Override
	protected Class<Incidente> getDomainClass() {
		return Incidente.class;
	}

	@SuppressWarnings("unchecked")
	public List<Incidente> getIncidentsByUser(int i){
		SessionFactory sessionFactory=this.getSessionFactory();
		Session session=sessionFactory.openSession();
		Criteria cr = session.createCriteria(Incidente.class);
		cr.add(Restrictions.eq("idUsuarioSol", i));
		cr.add(Restrictions.not(Restrictions.in("idEstado", new Integer[]{5,2})));
		List<Incidente> list = cr.list();		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Incidente> getIncidentsByUserAdmin(int i){
		SessionFactory sessionFactory=this.getSessionFactory();
		Session session=sessionFactory.openSession();
		Criteria cr = session.createCriteria(Incidente.class);
		cr.add(Restrictions.eq("idUsuarioSol", i));
		List<Incidente> list = cr.list();		
		return list;
	}
	
	public List<Incidente> getIncidentsBetweenDates(int fechaInicio, int fechaFin){
		
		return getHibernateTemplate().find("from Incidente where fechaInicio>=" + fechaInicio +"and fechaInicio<=" + fechaFin);
	}
	
	public List<Incidente> getIncidentsById(Long i){
		return getHibernateTemplate().find("from Incidente where id=" + i);
	}
	
	public List<Incidente> getIncidentsByClient(int i){
		return getHibernateTemplate().find("from Incidente where idCliente = " + i);
	}
	
	/* Method to UPDATE solution for a ticket */
	   public int updateTicketSolution(Integer id, String solucion){
		   	SessionFactory sessionFactory=this.getSessionFactory();
	        Session session=sessionFactory.openSession();
	        org.hibernate.Transaction transaction= session.beginTransaction();
	        Solucion s = new Solucion();
	        s.setDetalleSol(solucion);
	        s.setIdIncidente(id);
	        Incidente incidente=(Incidente) session.get(Incidente.class, id);
	        s.setIdUsuario(incidente.getIdUsuarioSol());
	        session.saveOrUpdate(s);
	        
	        try {
				transaction.commit();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}
	        session.close();
	        System.out.println("Updated");
	        return 1;
	   }
	   
	   /* Method to UPDATE state for a ticket */
	   public int updateTicketState(Integer id, Integer estado){
		   	SessionFactory sessionFactory=this.getSessionFactory();
	        Session session=sessionFactory.openSession();
	        org.hibernate.Transaction transaction= session.beginTransaction();
	        	        
	        Incidente incidente=(Incidente) session.get(Incidente.class, id);
	        incidente.setIdEstado(estado);
	        if (estado== 5){
	        	incidente.setFechaFin(new Date());
	        }
	        session.update(incidente);
	        
	        try {
				transaction.commit();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}
	        session.close();
	        System.out.println("Updated");
	        return 1;
	   }
	   
	   /* Method to UPDATE ticket */
	   public int updateTicket(Integer idIncidente, int idTecnico, int estado){
		   	SessionFactory sessionFactory=this.getSessionFactory();
	        Session session=sessionFactory.openSession();
	        org.hibernate.Transaction transaction= session.beginTransaction();
	        	        
	        Incidente incidente=(Incidente) session.get(Incidente.class, idIncidente);
	        incidente.setIdEstado(estado);
	        if (estado== 5){
	        	incidente.setFechaFin(new Date());
	        }
	        incidente.setIdUsuarioSol(idTecnico);
	        session.update(incidente);
	        
	        try {
				transaction.commit();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}
	        session.close();
	        System.out.println("========= Updated ticket =======");
	        return 1;
	   }
	   
	  
	   
}
